﻿using MY.ShoppingBackstage.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MY.ShoppingBackstage.BLL
{
    /// <summary>
    /// 商品信息
    /// </summary>
    public class Product_SpuService : BaseService<Product_Spu>
    {
        /// <summary>
        /// 添加商品
        /// </summary>
        /// <param name="productS"></param>
        /// <returns></returns>
        public bool AddProduct(Product_Spu product, List<Specs> specs)
        {
            using (var db = new WaiShopEntities())
            {
                //开启事务
                using (var tran = db.Database.BeginTransaction())
                {
                    try
                    {
                        //添加商品
                        product.InDate = DateTime.Now;
                        db.Product_Spu.Add(product);
                        db.SaveChanges();

                        foreach (var item in specs)
                        {
                            //添加规格
                            Spec spec = new Spec()
                            {
                                Spec_Name = item.Spec_Name,
                                CreateTime = DateTime.Now,
                                ModifiedTime = DateTime.Now
                            };
                            db.Spec.Add(spec);
                            db.SaveChanges();
                            foreach (var value in item.specValues)
                            {
                                //添加规格值
                                Spec_Value spec_Value = new Spec_Value()
                                {
                                    Spec_Id = spec.Id,
                                    Spec_value1 = value.name,
                                    CreateTime = DateTime.Now,
                                    ModifiedTime = DateTime.Now
                                };
                                db.Spec_Value.Add(spec_Value);
                                db.SaveChanges();
                                //添加商品sku表
                                Product_Sku product_Sku = new Product_Sku()
                                {
                                    ProductId = product.Id,
                                    Stock = value.num,
                                    Spec_Id = spec.Id,
                                    CreateTime = DateTime.Now,
                                    ModifiedTime = DateTime.Now
                                };
                                db.Product_Sku.Add(product_Sku);
                                db.SaveChanges();
                                //添加商品sku规格值关联表
                                Produc_Sku produc_Sku = new Produc_Sku()
                                {
                                    Product_Sku_Id = product_Sku.Id,
                                    Spec_Value_Id = spec_Value.Id,
                                    CreateTime = DateTime.Now,
                                    ModifiedTime = DateTime.Now
                                };
                                db.Produc_Sku.Add(produc_Sku);
                                db.SaveChanges();
                            }
                            //添加商品spu规格关联表
                            Produc_Spu_Spec produc_Spu_Spec = new Produc_Spu_Spec()
                            {
                                Produc_Spu_Id = product.Id,
                                Spec_Id = spec.Id,
                                CreateTime = DateTime.Now,
                                ModifiedTime = DateTime.Now
                            };
                            db.Produc_Spu_Spec.Add(produc_Spu_Spec);
                            db.SaveChanges();

                        }
                        tran.Commit();
                    }
                    catch (Exception)
                    {
                        tran.Rollback();
                        return false;
                        //throw;
                    }
                }
                return true;
            }
        }

        /// <summary>
        /// 显示数据
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="name"></param>
        /// <param name="datetime"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public List<Product_SpuView> GetProducts()
        {
            List<Product_SpuView> ps = new List<Product_SpuView>();
            using (var db = new WaiShopEntities())
            {
                var result = from p in db.Product_Spu
                             join c in db.Category
                             on p.Two_Category_Id equals c.ParentId
                             where c.Id == p.Three_Category_Id
                             select new Product_SpuView()
                             {
                                 Id = p.Id,
                                 Code = p.Code,
                                 Name = p.Name,
                                 categoryName = c.Name,
                                 Price = p.Price,
                                 Publish_Status = p.Publish_Status,
                                 Audit_Status = p.Audit_Status,
                                 InDate = p.InDate

                             };

                return result.ToList();
            }
        }
        /// <summary>
        /// 根据id删除指定商品信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delete(int id)
        {
            return true;
        }


        /// <summary>
        /// 根据商品Id得到对应规格和值
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public List<Specs> GetSpecs(int Id)
        {
            using (var db = new WaiShopEntities())
            {
                var result = from a in db.Spec
                             join b in db.Spec_Value.Distinct()
                             on a.Id equals b.Spec_Id
                             join c in db.Produc_Spu_Spec
                             on a.Id equals c.Spec_Id
                             join d in db.Product_Sku
                             on a.Id equals d.Spec_Id
                             where d.ProductId == Id && c.Produc_Spu_Id == Id
                             select new Specs()
                             {
                                 Spec_Name = a.Spec_Name,
                                 specValues = new List<Val>() {
                                     new Val(){
                                         name=b.Spec_value1,
                                         num=d.Stock
                                     }
                                 }
                             };

                return result.ToList();
            }
        }

        /// <summary>
        /// 修改商品
        /// </summary>
        /// <param name="product"></param>
        /// <param name="specs"></param>
        /// <returns></returns>
        //public bool Update(Product_Spu product, List<SpecS> specs)
        //{
        //    using (var db = new ShoppingBackstageEntities())
        //    {
        //        //开启事务
        //        using (var tran = db.Database.BeginTransaction())
        //        {
        //            try
        //            {
        //                //添加商品
        //                product.ModifiedTime = DateTime.Now;
        //                db.Product_Spu.Add(product);
        //                db.SaveChanges();

        //                foreach (var item in specs)
        //                {
        //                    //添加规格表
        //                    Spec spec = new Spec();
        //                    spec.Spec_Name = item.Spec_Name;
        //                    spec.CreateTime = DateTime.Now;
        //                    db.Spec.Add(spec);
        //                    db.SaveChanges();
        //                    //添加sku表
        //                    Product_Sku sku = new Product_Sku();
        //                    sku.ProductId = product.Id;
        //                    sku.Spec_Id = spec.Id;
        //                    sku.Stock = item.Stock;
        //                    sku.CreateTime = DateTime.Now;
        //                    db.Product_Sku.Add(sku);
        //                    db.SaveChanges();
        //                    //添加商品和规格管理关联表
        //                    Produc_Spu_Spec produc_Spu_Spec = new Produc_Spu_Spec();
        //                    produc_Spu_Spec.Produc_Spu_Id = product.Id;
        //                    produc_Spu_Spec.Spec_Id = spec.Id;
        //                    produc_Spu_Spec.CreateTime = DateTime.Now;
        //                    db.Produc_Spu_Spec.Add(produc_Spu_Spec);
        //                    db.SaveChanges();
        //                    foreach (var specvalues in item.specValues)
        //                    {
        //                        //循环遍历添加规格值
        //                        Spec_Value spec_Value = new Spec_Value();
        //                        spec_Value.CreateTime = DateTime.Now;
        //                        spec_Value.Spec_Id = spec.Id;
        //                        spec_Value.Spec_value1 = specvalues.Spec_value1;
        //                        db.Spec_Value.Add(spec_Value);
        //                        db.SaveChanges();
        //                        //商品sku规格值关联表
        //                        Produc_Sku produc_Sku = new Produc_Sku();
        //                        produc_Sku.CreateTime = DateTime.Now;
        //                        produc_Sku.Product_Sku_Id = product.Id;
        //                        produc_Sku.Spec_Value_Id = spec_Value.Id;
        //                        db.Produc_Sku.Add(produc_Sku);
        //                        db.SaveChanges();
        //                    }

        //                }
        //                tran.Commit();
        //            }
        //            catch (Exception)
        //            {
        //                tran.Rollback();
        //                return false;
        //                //throw;
        //            }
        //        }
        //        return true;
        //    }
        //}

    }
}
